const app = getApp();
var util = {
    counter: 0,

    formatDateTime: function (date) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        minute = minute < 10 ? ('0' + minute) : minute;
        var second = date.getSeconds();
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
    },

    // 传入时间 转化成  五年前的时间类型
    formatDateTimeFive: function (date) {
        var y = date.getFullYear() - 5;
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        minute = minute < 10 ? ('0' + minute) : minute;
        var second = date.getSeconds();
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
    },

    formatDateTimeYear: function (date) {
        var y = date.getFullYear();
        return y
    },

// 传入时间 转化成  五年前的年份
    formatDateTimeYearFive: function (date) {
        var y = date.getFullYear() - 5;
        return y
    },

//选择时间不能大于当前时间
    judgeDateTime: function (date) {
        let dataTime = new Date().getTime();
        let dataValueTime = new Date(date.replace(/-/g, '/')).getTime()
        if (dataValueTime > dataTime) {
            util.showError('选择时间不能大于当前时间');
            return false;
        } else {
            return true;
        }
    },


//获取年月日类型
    formatDateTimeNYR: function (date) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        return y + '-' + m + '-' + d;
    },

//教师处理学生写实,成功1，失败给错误提示
    dealRecord: function (postdata) {
        var that = this;
        const promise = new Promise(function (resolve, reject) {
            wx.request({
                url: app.ZsServerAddress + '/facts/dealRecord',
                method: 'POST',
                data: postdata,
                dataType: 'json',
                header: {
                    'Authorization': 'Bearer ' + app.token,
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                success: function (res) {
                    //教师处理写实接口返回成功
                    console.log('教师处理写实接口success----', res);
                    if (res.data.code == 0) {

                        resolve(1);

                    } else {
                        reject(res.data.message);
                    }

                },
                fail: function (res) {
                    that.showError(res.errMsg);
                    reject(res);
                },
            });
        });
        return promise;
    },


//学生删除   测试成功
    invalid: function (instanceId) {
        var that = this;
        wx.request({
            url: app.ZsServerAddress + '/facts/invalid',
            method: 'POST',
            data: {
                instanceId: instanceId,
            },
            dataType: 'json',
            header: {
                'Authorization': app.token,
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            success: function (res) {
                console.log('success----', res)
                if (res.data.code == 0) {
                    wx.showToast({
                        title: '作废成功',
                        icon: 'success',
                        duration: 2000,
                        success() {
                            wx.navigateBack({
                                delta: 1
                            })
                        },
                    });
                } else {
                    that.showError(res.data.message);
                }
            },
            fail: function (res) {
                that.showError(res.errMsg);
            }
        });
    },

//学生获取实例详情  测试成功
    getDetail: function (instanceId) {
        var that = this;
        const promise = new Promise(function (resolve, reject) {
            wx.request({
                url: app.ZsServerAddress + '/facts/getDetail',
                method: 'POST',
                data: {
                    //传输实例id
                    instanceId: instanceId,
                },
                dataType: 'json',
                header: {
                    'Authorization': 'Bearer ' + app.token,
                    'Apollo-Platform-Type': 'apollo-mobile',
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                success: function (res) {
                    if (res.data.code == 0) {
                        //获取写实记录详情成功
                        console.log('获取写实记录详情success----', res)

                        let instanceInfo = res.data.info.instanceInfo;

                        resolve(instanceInfo);

                    } else {
                        that.showError(res.data.message);
                    }
                },
                fail: function (res) {
                    that.showError(res.errMsg);
                    reject(res);
                }
            });
        });
        return promise;
    },
//预览图片  测试成功
    selectImg: function (instanceId) {
        var that = this;
        const promise = new Promise(function (resolve, reject) {
            wx.request({
                url: app.ZsServerAddress + '/facts/getDetail',
                method: 'POST',
                data: {
                    //传输实例id
                    instanceId: instanceId,
                },
                dataType: 'json',
                header: {
                    'Authorization': 'Bearer ' + app.token,
                    'Apollo-Platform-Type': 'apollo-mobile',
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                success: function (res) {
                    if (res.data.code == 0) {
                        console.log('获取写实记录详情success----', res)
                        let _data = res.data.info.instanceInfo.data;
                        var files = JSON.parse(_data).files;
                        resolve(files);
                        ;
                    } else {
                        that.showError(res.data.message);
                    }
                },
                fail: function (res) {
                    that.showError(res.errMsg);
                    reject(res);
                }
            });
        });
        return promise;
    },


//上传图片
    uploadFile: function (url) {
        var that = this
        const promise = new Promise(function (resolve, reject) {
            //上传文件
            wx.uploadFile({
                url: app.ImgServerAddress + '/upload',
                filePath: url,
                name: 'file',
                header: {
                    'Authorization': 'Bearer ' + app.token,
                    "Content-Type": "multipart/form-data",
                },
                success: function (res) {
                    //string 转json
                    var datajson = JSON.parse(res.data);
                    if (datajson.code == 0) {

                        resolve(datajson.info.url);

                    } else {
                        that.showError(res.data.message);
                    }
                },
                fail: function (res) {
                    that.showError("上传失败");
                    reject(res);
                }
            });
        });
        return promise;
    },

//学生提交，修改  
    submitRecord: function (data) {
        var that = this;
        wx.request({
            url: app.ZsServerAddress + '/facts/submitRecord',
            header: {
                'Authorization': 'Bearer ' + app.token,
                'Content-Type': 'application/json',
            },
            method: 'POST',
            data: data,
            dataType: 'json',
            success: function (res) {
                if (res.data.code == 0) {
                    that.showSuccess("提交成功");
                } else {
                    that.showError(res.data.message);
                }
            },
            fail: function (res) {
                that.showError(res.errMsg);
            },
        });
    },

//登录接口
    loginByDingTalk: function (secret) {
        var that = this;
        const promise = new Promise(function (resolve, reject) {
            wx.request({
                url: app.ZsServerAddress + '/user/loginByDingTalk',
                method: 'POST',
                data: {
                    //领卡秘钥
                    secret: secret,
                },
                dataType: 'json',
                header: {
                    'Content-Type': 'application/json',
                },
                success: function (res) {
                    if (res.data.code == 0) {
                        //登录成功
                        resolve(res);
                    } else {
                        that.showError(res.data.message);
                    }
                },
                fail: function (res) {
                    that.showError(res.errMsg);
                    reject(res);
                }
            });
        });
        return promise;
    },

//老师获取首页实例
    getTeacherIndex: function (data) {
        var that = this;
        const promise = new Promise(function (resolve, reject) {
            wx.request({
                url: app.ZsServerAddress + '/facts/getTeacherIndex',
                method: 'POST',
                data: data,
                dataType: 'json',
                header: {
                    'Authorization': 'Bearer ' + app.token,
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                success: function (res) {
                    //成功
                    resolve(res);
                },
                fail: function (res) {
                    console.log("httpRequestFail---", res)
                    util.showError(JSON.stringify(res.errMsg))
                    reject(res);
                },
                complete: function (res) {
                    util.hideLoading()
                }
            });
        });
        return promise;
    },

//获取用户信息
    getUserInfo: function () {
        var that = this;
        const promise = new Promise(function (resolve, reject) {
            wx.request({
                url: app.ZsServerAddress + '/user/info',
                method: 'POST',
                dataType: 'json',
                header: {
                    'Authorization': 'Bearer ' + app.token,
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                success: function (res) {
                    app.Role = res.data.info.role.loginRoleId;
                    resolve(res);
                },
                fail: function (res) {
                    that.showError(res.errMsg);
                    reject(res);
                }
            });
        });
        return promise;
    },


//获取用户信息
    getUserInfoLogin: function () {
        var that = this;
        const promise = new Promise(function (resolve, reject) {
            wx.request({
                url: app.ZsServerAddress + '/user/info',
                method: 'POST',
                dataType: 'json',
                header: {
                    'Authorization': 'Bearer ' + app.token,
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                success: function (res) {
                    //成功
                    if (res.data.code == 0) {
                        console.log("------------+++++++++++++:" + app.Role);
                        app.Role = res.data.info.role.loginRoleId;
                        console.log("--------------------:" + app.Role);
                        wx.reLaunch({
                            url: '/pages/index/index'
                        })
                    }
                    resolve(res);
                },
                fail: function (res) {
                    that.showError(res.errMsg);
                    reject(res);
                }
            });
        });
        return promise;
    },

//修改用户信息
    updateUser: function (data) {
        var that = this;
        const promise = new Promise(function (resolve, reject) {
            wx.request({
                url: app.ZsServerAddress + '/user/updateUser',
                method: 'POST',
                data: data,
                dataType: 'json',
                header: {
                    'Authorization': 'Bearer ' + app.token,
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                success: function (res) {
                    //成功
                    resolve(res);
                },
                fail: function (res) {
                    that.showError(res.errMsg);
                    reject(res);
                }
            });
        });
        return promise;
    },
    //获取学期列表
    getSemesterList: function () {
        const promise = new Promise(function (resolve, reject) {
            wx.request({
                url: app.ZsServerAddress + '/config/getSemesterList',
                method: 'POST',
                dataType: 'json',
                header: {
                    'Authorization': 'Bearer ' + app.token,
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    this.showError(res);
                    reject(res);
                },
            });
        });
        return promise;
    },

    //开卡
    addCard: function (data) {
        const promise = new Promise(function (resolve, reject) {
            wx.request({
                url: app.LkServerAddress + '/dingtalk/addCard',
                method: 'POST',
                data: data,
                dataType: 'json',
                header: {
                    'Authorization': app.cardToken,
                    'Content-Type': 'application/json',
                },
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    this.showError(res.errMsg);
                    reject(res);
                },
            });
        });
        return promise;
    },

    //获取我的列表
    getHomeList: function (httpUrl) {
        var me = this;
        const promise = new Promise(function (resolve, reject) {
            wx.request({
                url: httpUrl,
                method: 'POST',
                dataType: 'json',
                header: {
                    'Authorization': 'Bearer ' + app.token,
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    me.showError(res.errMsg);
                    reject(res);
                },
            });
        });
        return promise;
    },


    // 获取待处理和已处理的条数   dealType：1待处理 2已处理
    getSize: function (dealType) {
        const promise = new Promise(function (resolve, reject) {
            wx.request({
                url: app.ZsServerAddress + '/facts/getTeacherIndex',
                method: 'POST',
                data: {
                    page: 1,
                    pageSize: 1,
                    dealType: dealType,
                },
                dataType: 'json',
                header: {
                    'Authorization': 'Bearer ' + app.token,
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                success: function (res) {
                    console.log('获取写实记录size', res)
                    if (res.data.code == 0) {
                        resolve(res.data.info.total || 0);
                    } else if (res.data.code == 3003) {
                        resolve(0);
                    } else {
                        util.showError(JSON.stringify(res.data.message))
                    }
                },
                fail: function (res) {
                    util.showError(JSON.stringify(res.errMsg))
                    reject(res);
                },
            });
        });
        return promise;
    },


    showSuccess: function (message) {
        wx.showToast({
            title: message,
            icon: 'success',
            duration: 2000,
            success() {
                wx.navigateBack({
                    delta: 1
                })
            },
        })
    },
    showError: function (message) {
        wx.showToast({
            title: message,
            icon: 'none',
            duration: 3000,
        })
    },

    showModal: function (message) {
        wx.showModal({
            title: '提示',
            content: message,
        })
    },


    arrList: {
        itemName: "请输入【名称】",
        organizer: "请输入【单位】",
        itemContent: "请输入【内容】",
        beginTime: "请选择【开始时间】",
        endTime: "请选择【结束时间】",
        honorType: "请选择【级别】",
        honorName: "请输入【表彰名称】",
        honorDate: "请选择【获奖时间】",
        punishmentName: "请输入【名称】",
        punishmentContent: "请输入【事由】",
        punishmentDate: "请选择【处分时间】",
        otherName: "请输入【名称】",
        otherContent: "请输入【情况说明】",
        studiesEvent: "请输入【学习实例】/【创新成果】",
        studiesContent: "请输入【主要内容】",
        competitionType: "请输入【竞赛类型】",
        competitionName: "请输入【竞赛名称】",
        honorlevel: "请输入【奖次】",
        organizations: "请输入【团名称】",
        examines: "请选择【考核情况】",
        specialskill: "请输入【特长项目】",
        pracName: "请输入【实践名称】",
        station: "请输入【岗位】",
        stationType: "请输入【级别】",
        researchName: "请输入【研学主题名称】",
        researchAdress: "请输入【研学地点】",
        summaries: "请输入【个人总结】",
    },
    // 非空验证
    validation: function (obj) {
        for (let key in obj) {

            if (obj[key] == "") {
                util.showError(this.arrList[key])
                return false;
            }
        }
        return true;
    },

    //showLoadingIndex>=hideLoadingIndex  显示
    showLoadingIndex: 0,
    hideLoadingIndex: 0,

    // showIndex:0,
    // hideIndex:0,

    //延迟一秒后才显示加载图标，否则不出现
    showLoading() {
        // let showI=this.showIndex
        // showI=showI+1
        setTimeout(() => {
            // console.error('showIndex:'+showI,'showLoadingIndex:'+this.showLoadingIndex,'hideLoadingIndex:'+this.hideLoadingIndex)
            // this.showIndex=showI
            if(this.showLoadingIndex >= this.hideLoadingIndex)
            {
                wx.showLoading({
                    title: '加载中...',
                });
            }
            this.showLoadingIndex = this.showLoadingIndex + 1
        },1000)
    },

    hideLoading() {
        // let hideI=this.hideIndex
        // hideI=hideI+1
        // setTimeout(() => {
        // console.error('hideIndex:'+hideI,'showLoadingIndex:'+this.showLoadingIndex,'hideLoadingIndex:'+this.hideLoadingIndex)
        // this.hideIndex=hideI
        wx.hideLoading();
        this.hideLoadingIndex = this.hideLoadingIndex + 1
        // },0)
    },
};
export {util};

